package com.ahzy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.ahzy.po.Notice;
import com.ahzy.service.NoticeService;
import com.ahzy.vo.NoticeAndUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@RestController
@RequestMapping("/notice")
@Slf4j
@Api
public class NoticeContriller {

	@Autowired
	NoticeService noticeService;
	

	@ApiOperation(value = "获取列表分页数据")
	@GetMapping("/data")
	public Map<String, Object> data(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
			@RequestParam(value = "title", required = false) String title) {
		Map<String, Object> mapData = new HashMap<>();
		Example noticeExample = new Example(Notice.class);
		Criteria noticeCriteria = noticeExample.createCriteria();
		//添加模糊查询
		if(title!=null) {
			noticeCriteria.andLike("title", "%" + title+ "%");
		}
		
		
		PageInfo<Notice> rolePageInfo = noticeService.selectByExamplePage(page, limit, noticeExample);
		mapData.put("code", 0);
		mapData.put("msg", "");
		mapData.put("state", true);
		mapData.put("total", rolePageInfo.getTotal());
		mapData.put("data", rolePageInfo.getList());
		return mapData;
	}

	@ApiOperation(value = "获取列表分页数据")
	@GetMapping("/noticeAndUserdata")
	public Map<String, Object> noticeAndUserdata(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
			@RequestParam(value = "title", required = false, defaultValue = "") String title) {
		Map<String, Object> mapData = new HashMap<>();
		PageHelper.startPage(page, limit);
		PageInfo<NoticeAndUser> userPageInfo = new PageInfo<>(noticeService.selectByUserAndNoticeExamplePage(title));
		mapData.put("code", 0);
		mapData.put("msg", "");
		mapData.put("state", true);
		mapData.put("data", userPageInfo);
		return mapData;
	}
	
	@ApiOperation(value = "有主键则修改，否则新增")
	@GetMapping("/save")
	public Map<String, Object> save(@Validated Notice Notice) {
		System.out.println(Notice);
		Map<String, Object> mapData = new HashMap<>();
		try {
			if (Notice.getId() == null) {
				// 新增操作
				noticeService.insertWithoutPrimaryKey(Notice);
				mapData.put("state", true);
			} else {
				// 修改操作
				noticeService.updateByPrimaryKeySelective(Notice);
				mapData.put("state", true);
			}
		} catch (Exception e) {
			mapData.put("state", false);
			log.error("{}", e.getMessage());
		}
		return mapData;
	}

	@ApiOperation(value = "通过id删除管理员信息")
	@DeleteMapping("/remove/{id}")
	public Map<String, Object> delete(@PathVariable Integer id) {
		Map<String, Object> mapData = new HashMap<>();
		try {
			noticeService.deleteByPrimaryKey(id);
			mapData.put("state", true);
		} catch (Exception e) {
			mapData.put("state", false);
			log.error("delete()方法发生异常：{}", e.getMessage());
		}
		return mapData;
	}

	@ApiOperation(value = "通过id查看管理员信息")
	@GetMapping("/detail/{id}")
	public Map<String, Object> detail(@PathVariable Integer id) {
		Map<String, Object> mapData = new HashMap<>();
		Notice Notice = noticeService.selectByPrimaryKey(id);
		mapData.put("state", true);
		mapData.put("data", Notice);
		return mapData;
	}

}
